﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;

namespace PkwDevFramework.Common
{
    public class BaseService<T>
        where T : class, new()
    {
        public Repository<T> Repository { get; }

        public BaseService(Repository<T> repository)
        {
            Repository = repository;
        }

        #region 查询


        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public List<T> GetList(Expression<Func<T, bool>> whereExpression)
        {
            return Repository.GetList(whereExpression);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereExpression">条件</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小/内容数量</param>
        /// <param name="totalCount">页数</param>
        /// <returns></returns>
        public List<T> GetPageList(
            Expression<Func<T, bool>> whereExpression,
            int pageIndex,
            int pageSize,
            out int totalCount
        )
        {
            var pageModel = new PageModel { PageIndex = pageIndex, PageSize = pageSize };
            var list = Repository.GetPageList(whereExpression, pageModel);
            totalCount = pageModel.TotalCount;
            return list;
        }

        /// <summary>
        /// 条件查询-返回单条
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public T GetSingle(Expression<Func<T, bool>> whereExpression)
        {
            return Repository.GetSingle(whereExpression);
        }
        #endregion

        #region 插入

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Insert(T entity)
        {
            return Repository.Insert(entity);
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool Insert(List<T> list)
        {
            return Repository.InsertRange(list);
        }
        #endregion

        #region 更新

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            return Repository.Update(entity);
        }

        public bool Update(
            Expression<Func<T, T>> expression,
            Expression<Func<T, bool>> whereExpression
        )
        {
            return Repository.Update(expression, whereExpression);
        }
        #endregion


        #region 删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            return Repository.Delete(entity);
        }

        /// <summary>
        /// 条件删除
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public bool Delete(Expression<Func<T, bool>> whereExpression)
        {
            return Repository.Delete(whereExpression);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool Delete(List<T> list)
        {
            return Repository.Delete(list);
        }
        #endregion
    }
}
